package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.*;
import com.example.sys.mapper.RoleMapper;
import com.example.sys.service.MenuService;
import com.example.sys.service.RoleDeptService;
import com.example.sys.service.RoleService;
import com.example.sys.vo.RoleVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-17
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleDeptService roleDeptService;


    @PostMapping("/addRole")
    @RequiresPermissions({"system:role:add"})
    public ResponseBean<UnicomResponseEnums> addRole(@RequestBody @Valid Role role){
        Map<String,Object> objectStringMap=BeanUtil.beanToMap(role, false, true);
        if (objectStringMap.isEmpty())
            return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);

        roleService.save(role);
        return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
    }

    @DeleteMapping("/deleteByRoleId")
    @RequiresPermissions({"system:role:remove"})
    public ResponseBean<UnicomResponseEnums> deleteByRoleId(@RequestBody @Valid Role role){
        if (role.getRoleId()==1){
            return new ResponseBean(true, UnicomResponseEnums.ADMINISTRATOR_ROLE_NOT_DELETE);
        }
        Boolean count=roleService.removeById(role.getRoleId());
        if (!count)
            return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
        return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
    }

    @DeleteMapping("/deleteRoles")
    @RequiresPermissions({"system:role:remove"})
    public ResponseBean<UnicomResponseEnums> deleteRoles(List<Role> roles){
        if (!roles.isEmpty()) {
            for (Role roleId : roles) {
                deleteByRoleId(roleId);
            }
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateRoleByRoleId")
    @RequiresPermissions({"system:role:edit"})
    public ResponseBean<UnicomResponseEnums> updateRoleByRoleId(@RequestBody @Valid Role role){
        if (role.getRoleId()==1){
            return new ResponseBean(false, UnicomResponseEnums.ADMINISTRATOR_ROLE_NOT_UPDATE);
        }
        Boolean bool=roleService.updateById(role);
        if (bool){
//            roleService.updateById(role);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @RequestMapping("/updateRoles")
    @RequiresPermissions({"system:role:edit"})
    public ResponseBean<UnicomResponseEnums> updateRoles(@Valid List<Role> roles){
        if (!roles.isEmpty()) {
            for (Role role : roles) {
                updateRoleByRoleId(role);
            }
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @GetMapping("/getRoleList")
    public ResponseBean<UnicomResponseEnums> getRoleList(RoleVo roleVo){
        System.out.println(roleVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(roleVo, false, true);
        if (stringObjectMap.isEmpty()){
            List<Role> list=roleService.list();
            return new ResponseBean(true, list, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, roleService.selectPage(roleVo), UnicomResponseEnums.SUCCESS_OPTION);
    }

    @RequestMapping("/getRole")
    public ResponseBean<UnicomResponseEnums> getRole(Long roleId){
        if (!roleId.toString().isEmpty()) {
            Role role=roleService.getById(roleId);
            return new ResponseBean(true, role, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateRoleDefaultJurisdiction")
    public ResponseBean<UnicomResponseEnums> updateRoleDefaultJurisdiction(@RequestBody Role role){
        if ("0".equals(role.getIsDefault())){
            role.setIsDefault("1");
            roleService.updateById(role);
        }else if ("1".equals(role.getIsDefault())){
            UpdateWrapper<Role> wrapper = new UpdateWrapper<>();
            wrapper.set("is_default", "1");
            roleService.update(wrapper);
            role.setIsDefault("0");
            roleService.updateById(role);
        }else {
            return new ResponseBean(false, UnicomResponseEnums.ROLE_DEFAULT_NON_CONFORMITY);
        }
        return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
    }

    @GetMapping("/fetchRoleInfo")
    public ResponseBean<UnicomResponseEnums> fetchRoleInfo(Long roleId){
        System.out.println(roleId);
        Map<String, Object> map = new HashMap<String, Object>();
        List<RoleMenu> menuList =roleService.getMenuList(roleId);
        System.out.println(menuList);
        map.put("menu", menuList);
        List<UserRole> userList = roleService.getUserList(roleId);
        map.put("user",userList);
        List<RoleDept> deptList= roleService.getDeptList(roleId);
        map.put("dept", deptList);
        return new ResponseBean(true,map, UnicomResponseEnums.SUCCESS_OPTION);
    }

    @PostMapping("/editDataScope")
    public ResponseBean<UnicomResponseEnums> editDataScope(@RequestBody Map<String,Object> map){

        System.out.println(map);
        String str = (String) map.get("dataScope");
        Long roleId=((Integer)map.get("roleId")).longValue();
        Role role=new Role();
        role.setRoleId(roleId);
        role.setDataScope(str);
        roleService.updateById(role);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", roleId);
        roleDeptService.remove(wrapper);

        return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
    }

}
